<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
  <head>
    <th:block th:replace="~{/_include/header}" />
    <title>ADMIN CLI</title>
    <style>
      html,
      body,
      input,
      .rb-console .out {
        background-color: #222;
        color: #f1f1f1;
        font-family: SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;
        line-height: 1.5;
        font-size: 12px;
      }
      .rb-console {
        padding: 20px;
      }
      .rb-console input {
        width: 100%;
        padding: 6px 2px;
        border: 0 none;
        appearance: none;
        -webkit-appearance: none;
        outline: none;
        border-bottom: 1px solid rgba(255, 255, 255, 0.2);
      }
      .rb-console .out {
        margin-top: 20px;
      }
      .rb-console .out div.cm::before {
        content: '#';
        width: 16px;
        display: inline-block;
        animation: flash 1s infinite;
      }
      .rb-console .out div.cm.ok::before {
        animation: none;
      }
    </style>
  </head>
  <body>
    <div class="rb-console">
      <div class="in">
        <input type="text" placeholder="# enter command ... eg: help" />
      </div>
      <div class="out"></div>
    </div>
    <th:block th:replace="~{/_include/footer}" />
    <script type="text/babel">
      function _print(c, o) {
        const $c = $(`<div class="${o ? '' : 'cm'}">${c.replace(/\n/g, '<br>')}</div>`)
        $('.out').prepend($c)
        return $c
      }

      $(document).ready(() => {
        const $in = $('.in>input').keydown((event) => {
          if (event.which === 13 && $in.val()) {
            const c = $in.val().trim()
            $in.val('')
            if (c === 'clean') {
              $('.out').empty()
              return
            }

            const $c = _print(c)
            $.post('/admin/admin-cli/exec', c, (o) => {
              $c.addClass('ok')
              if (o && o.data) _print(o.data, true)
              else if (o && o.error_msg) _print(`ERROR: ${o.error_msg}`, true)
            })
          }
        })
        $in.focus()
      })
    </script>
  </body>
</html>
